package com.hai.prism.driver;

import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.org.apache.xml.internal.security.Init;

public class AS7343 {
	
    private static CH341 CH341 = null;
    private long   iIndex = 0;
    private int    measure_mode = 0;      //  0  eSpm  1  eSyns  3  eSynd
    
    private AS7343() {};
    
    /**
     * 加载CH341动态库API，实例化CH341
     * */
    static {
        String bits = System.getProperty("sun.arch.data.model");                                    // 获取jdk位数
        System.out.println("jdk位数\t" + bits);
        String ops = System.getProperty("os.name");                                                 // 获取os名称
        System.out.println("操作系统：\t" + ops);
        if (ops.startsWith("win") || ops.startsWith("Win")){                                        // windows
            if ("32".equals(bits)) {
                System.out.println("程序加载了:\tCH341DLLA.dll");
                System.load(System.getProperty("user.dir") + "\\lib\\ch341\\CH341DLLA64.DLL");
                CH341 = (CH341) Native.loadLibrary("CH341DLLA", CH341.class);
 
            } else {
                System.out.println("程序加载了:\tCH341DLLA64.dll");
                System.load(System.getProperty("user.dir") + "\\lib\\ch341\\CH341DLLA64.DLL");
                CH341 = (CH341) Native.loadLibrary("CH341DLLA64", CH341.class);
            }
 
        } else {                                                                                  // 其他操作系统  // 去WSC官方下载CH341对应操作系统驱动即可支持本地操作系统
            System.out.println("当前仅支持Windows系统，暂不支持其他操作系统！");
        }
    }
    
    public AS7343(long iIndex, int measure_mode) {
    	this.iIndex = iIndex;
    	this.measure_mode = measure_mode;
    }
    
    public static final byte ADDR            = (byte)0x39;
	  
    public static final byte REG_CONFIG      = (byte)0x70;
    public static final byte REG_STAT        = (byte)0x71;
    public static final byte REG_EDGE        = (byte)0x72;
    public static final byte REG_CPIO        = (byte)0x73;
    public static final byte REG_LED         = (byte)0x74;
    													  
    public static final byte REG_ENABLE      = (byte)0x80;
    public static final byte REG_ATIME       = (byte)0x81;
    public static final byte REG_WTIME       = (byte)0x83;
    													  
    public static final byte REG_SP_TH_L_LSB = (byte)0x84;
    public static final byte REG_SP_TH_L_MSB = (byte)0x85;
    public static final byte REG_SP_TH_H_LSB = (byte)0x86;
    public static final byte REG_SP_TH_H_MSB = (byte)0x87;
    public static final byte REG_AUXID       = (byte)0x90;
    public static final byte REG_REVID       = (byte)0x91;
    													  
    public static final byte REG_ID          = (byte)0x92;
    public static final byte REG_STATUS_1    = (byte)0x93;
    public static final byte REG_ASTATUS     = (byte)0x94;
    													  
    public static final byte REG_CH0_DATA_L  = (byte)0x95;
    public static final byte REG_CH0_DATA_H  = (byte)0x96;
    public static final byte REG_CH1_DATA_L  = (byte)0x97;
    public static final byte REG_CH1_DATA_H  = (byte)0x98;
    public static final byte REG_CH2_DATA_L  = (byte)0x99;
    public static final byte REG_CH2_DATA_H  = (byte)0x9A;
    public static final byte REG_CH3_DATA_L  = (byte)0x9B;
    public static final byte REG_CH3_DATA_H  = (byte)0x9C;
    public static final byte REG_CH4_DATA_L  = (byte)0x9D;
    public static final byte REG_CH4_DATA_H  = (byte)0x9E;
    public static final byte REG_CH5_DATA_L  = (byte)0x9F;
    public static final byte REG_CH5_DATA_H  = (byte)0xA0;

    public static final byte REG_STATUS_2    = (byte)0xA3;
    public static final byte REG_STATUS_3    = (byte)0xA4;
    public static final byte REG_STATUS_5    = (byte)0xA6;
    public static final byte REG_STATUS_6    = (byte)0xA7;
    public static final byte REG_CFG_0       = (byte)0xA9;
    public static final byte REG_CFG_1       = (byte)0xAA;
    public static final byte REG_CFG_3       = (byte)0xAC;
    public static final byte REG_CFG_6       = (byte)0xAF;
    public static final byte REG_CFG_8       = (byte)0xB1;
    public static final byte REG_CFG_9       = (byte)0xB2;
    public static final byte REG_CFG_10      = (byte)0xB3;
    public static final byte REG_CFG_12      = (byte)0xB5;

    public static final byte REG_PERS        = (byte)0xBD;
    public static final byte REG_GPIO_2      = (byte)0xBE;
    public static final byte REG_ASTEP_L     = (byte)0xCA;
    public static final byte REG_ASTEP_H     = (byte)0xCB;
    public static final byte REG_AGC_GAIN_MAX= (byte)0xCF;
    public static final byte REG_AZ_CONFIG   = (byte)0xD6;
    public static final byte REG_FD_TIME_1   = (byte)0xD8;
    public static final byte REG_TIME_2      = (byte)0xDA;
    public static final byte REG_CFG0        = (byte)0xD7;
    public static final byte REG_STATUS      = (byte)0xDB;
    public static final byte REG_INTENAB     = (byte)0xF9;
    public static final byte REG_CONTROL     = (byte)0xFA;
    public static final byte REG_FIFO_MAP    = (byte)0xFC;
    public static final byte REG_FIFO_LVL    = (byte)0xFD;
    public static final byte REG_FDATA_L     = (byte)0xFE;
    public static final byte REG_FDATA_H     = (byte)0xFF;

    public static final byte GPIO            = (byte)0x4; //待验证 long？
    
    public boolean readREG(byte reg, Pointer data) {
    	return !CH341.CH341ReadI2C(this.iIndex, AS7343.ADDR, reg, data);
	}
    
    public boolean writeREG(byte reg, byte data) {
    	return !CH341.CH341WriteI2C(this.iIndex, AS7343.ADDR, reg, data);
	}
    
    public byte readID() {
    	Pointer id = new Memory(1);
    	if (this.readREG(AS7343.REG_ID, id)) {
			return id.getByte(0);
		}
    	return -1;
    }
    
    public boolean enable(boolean enable) {
    	byte data = (byte) 0x0;
    	Pointer status = new Memory(1);
    	if (this.readREG(AS7343.REG_ENABLE, status)) {
        	data = status.getByte(0);
        	if (enable) {
    			data |= (1 << 0) ;
    		} else {
    			data &= ~(1 << 0);
    		}
    		return this.writeREG(AS7343.REG_ENABLE, data);
		}
    	return false;
    }
    
    public boolean enableSpectralMeasure(boolean enable) {
    	byte data = (byte) 0x0;
    	Pointer status = new Memory(1);
    	if (this.readREG(AS7343.REG_ENABLE, status)) {
        	data = status.getByte(0);
        	if (enable) {
    			data |= (1 << 1) ;
    		} else {
    			data &= ~(1 << 1);
    		}
    		return this.writeREG(AS7343.REG_ENABLE, data);
		}
    	return false;
    }
    
    public boolean enableWait(boolean enable) {
    	byte data = (byte) 0x0;
    	Pointer status = new Memory(1);
    	if (this.readREG(AS7343.REG_ENABLE, status)) {
        	data = status.getByte(0);
        	if (enable) {
    			data |= (1 << 3) ;
    		} else {
    			data &= ~(1 << 3);
    		}
    		return this.writeREG(AS7343.REG_ENABLE, data);
		}
    	return false;
    }
    
    public boolean enableSMUX(boolean enable) {
    	byte data = (byte) 0x0;
    	Pointer status = new Memory(1);
    	if (this.readREG(AS7343.REG_ENABLE, status)) {
        	data = status.getByte(0);
        	if (enable) {
    			data |= (1 << 4) ;
    		} else {
    			data &= ~(1 << 4);
    		}
    		return this.writeREG(AS7343.REG_ENABLE, data);
		}
    	return false;
    }
    
    public boolean enableFlickerDetection(boolean enable) {
    	byte data = (byte) 0x0;
    	Pointer status = new Memory(1);
    	if (this.readREG(AS7343.REG_ENABLE, status)) {
        	data = status.getByte(0);
        	if (enable) {
    			data |= (1 << 6) ;
    		} else {
    			data &= ~(1 << 6);
    		}
    		return this.writeREG(AS7343.REG_ENABLE, data);
		}
    	return false;
    }
    
    public boolean CompleteMeasure() {
    	byte status = (byte) 0x0;
    	Pointer data = new Memory(1);
    	if (this.readREG(AS7343.REG_STATUS_2, data)) {
    		status = data.getByte(0);
    		// System.out.println("status: " + status + " " + (status & (1 << 6)));
        	if (status == (1 << 6)) {
    			return true;
    		} 
		}
    	return false;
    }
    
    public boolean setBank(boolean addr) {
    	byte data = (byte) 0x0;
    	Pointer status = new Memory(1);
    	if (this.readREG(AS7343.REG_CFG_0, status)) {
        	data = status.getByte(0);
        	if (addr) {
    			data |= (1 << 4) ;
    		} else {
    			data &= ~(1 << 4);
    		}
    		return this.writeREG(AS7343.REG_ENABLE, data);
		}
    	return false;
    }
    
    public boolean config() {
    	int measure_mode = this.measure_mode;
    	
    	byte data = (byte) 0x0;
    	
    	this.setBank(true);
    	
    	Pointer status = new Memory(1);
    	if (this.readREG(AS7343.REG_CONFIG, status)) {
        	data = status.getByte(0);
        	
        	switch (measure_mode) {
			case 0:
				data = (byte) ((data & (~3)) | 0);
				break;
			case 1:
				data = (byte) ((data & (~3)) | 1);
				break;
			case 3:
				data = (byte) ((data & (~3)) | 3);
				break;
			default:
				break;
			}
        	
        	this.measure_mode = measure_mode; // 更改传感器默认工作模式为设定值
    		return this.writeREG(AS7343.REG_ENABLE, data) && this.setBank(false);
		}
    	return false;
    }
    
    public boolean f1_f4_clear_nir() {
    	return     this.writeREG((byte)0x00, (byte)0x30)
    			&& this.writeREG((byte)0x01, (byte)0x01)
    			&& this.writeREG((byte)0x02, (byte)0x00)
    			&& this.writeREG((byte)0x03, (byte)0x00)
    			&& this.writeREG((byte)0x04, (byte)0x00)
    			&& this.writeREG((byte)0x05, (byte)0x42)
    			&& this.writeREG((byte)0x06, (byte)0x00)
    			&& this.writeREG((byte)0x07, (byte)0x00)
    			&& this.writeREG((byte)0x08, (byte)0x50)
    			&& this.writeREG((byte)0x09, (byte)0x00)
    			&& this.writeREG((byte)0x0A, (byte)0x00)
    			&& this.writeREG((byte)0x0B, (byte)0x00)
    			&& this.writeREG((byte)0x0C, (byte)0x20)
    			&& this.writeREG((byte)0x0D, (byte)0x04)
    			&& this.writeREG((byte)0x0E, (byte)0x00)
    			&& this.writeREG((byte)0x0F, (byte)0x30)
    			&& this.writeREG((byte)0x10, (byte)0x01)
    			&& this.writeREG((byte)0x11, (byte)0x50)
    			&& this.writeREG((byte)0x12, (byte)0x00)
    			&& this.writeREG((byte)0x13, (byte)0x06);
    	
    }
    
    public boolean f5_f8_clear_nir() {
    	return     this.writeREG((byte)0x00, (byte)0x00)
    			&& this.writeREG((byte)0x01, (byte)0x00)
    			&& this.writeREG((byte)0x02, (byte)0x00)
    			&& this.writeREG((byte)0x03, (byte)0x40)
    			&& this.writeREG((byte)0x04, (byte)0x02)
    			&& this.writeREG((byte)0x05, (byte)0x00)
    			&& this.writeREG((byte)0x06, (byte)0x10)
    			&& this.writeREG((byte)0x07, (byte)0x03)
    			&& this.writeREG((byte)0x08, (byte)0x50)
    			&& this.writeREG((byte)0x09, (byte)0x10)
    			&& this.writeREG((byte)0x0A, (byte)0x03)
    			&& this.writeREG((byte)0x0B, (byte)0x00)
    			&& this.writeREG((byte)0x0C, (byte)0x00)
    			&& this.writeREG((byte)0x0D, (byte)0x00)
    			&& this.writeREG((byte)0x0E, (byte)0x24)
    			&& this.writeREG((byte)0x0F, (byte)0x00)
    			&& this.writeREG((byte)0x10, (byte)0x00)
    			&& this.writeREG((byte)0x11, (byte)0x50)
    			&& this.writeREG((byte)0x12, (byte)0x00)
    			&& this.writeREG((byte)0x13, (byte)0x06);
    	
    }
    
    public boolean fd_confiig() {
    	return     this.writeREG((byte)0x00, (byte)0x00)
    			&& this.writeREG((byte)0x01, (byte)0x00)
    			&& this.writeREG((byte)0x02, (byte)0x00)
    			&& this.writeREG((byte)0x03, (byte)0x00)
    			&& this.writeREG((byte)0x04, (byte)0x00)
    			&& this.writeREG((byte)0x05, (byte)0x00)
    			&& this.writeREG((byte)0x06, (byte)0x00)
    			&& this.writeREG((byte)0x07, (byte)0x00)
    			&& this.writeREG((byte)0x08, (byte)0x00)
    			&& this.writeREG((byte)0x09, (byte)0x00)
    			&& this.writeREG((byte)0x0A, (byte)0x00)
    			&& this.writeREG((byte)0x0B, (byte)0x00)
    			&& this.writeREG((byte)0x0C, (byte)0x00)
    			&& this.writeREG((byte)0x0D, (byte)0x00)
    			&& this.writeREG((byte)0x0E, (byte)0x00)
    			&& this.writeREG((byte)0x0F, (byte)0x00)
    			&& this.writeREG((byte)0x10, (byte)0x00)
    			&& this.writeREG((byte)0x11, (byte)0x00)
    			&& this.writeREG((byte)0x12, (byte)0x00)
    			&& this.writeREG((byte)0x13, (byte)0x60);
    }
    
    /**
     * 
     * @param mode
     * 		14  eF1F4ClearNIR
     *      58  eF5F8ClearNIR
     */
    public void startMeasure(int mode) {
    	byte data = (byte) 0x0;
    	Pointer status = new Memory(1);
    	if (this.readREG(AS7343.REG_CFG0, status)) {
        	data = status.getByte(0);
		}
    	data &= ~(1<<4);
    	this.writeREG(AS7343.REG_CFG0, data);
    	
    	this.enableSpectralMeasure(false);
    	
    	data = 0x10;
    	this.writeREG(AS7343.REG_CFG_6, data);
    	
    	if(mode == 14) {
    		this.f1_f4_clear_nir();
    		// System.out.println(this.f1_f4_clear_nir());
    	} else if (mode == 58) {
			this.f5_f8_clear_nir();
    		// System.out.println(this.f5_f8_clear_nir());
		} else {
			System.out.println("测量值不在支持范围内，可选值：14 58");
		}
   
    	this.enableSMUX(true);
    	if (this.measure_mode == 1) {        // eSyns
			// this.set_gpio_mode(INPUT);    // INPUT暂不支持
    		// this.config();
		} else if(this.measure_mode == 0) {  // eSpm
			this.config();
		}
    	this.enableSpectralMeasure(true);
    	
    	if (this.measure_mode == 0) {       // eSpm
			while (!this.CompleteMeasure()) {
				try {
					Thread.sleep(1*10);    //10ms
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
    }
    
    public int readFlickerData() {
    	byte flicker = 0;
    	byte data    = 0;
    	
    	Pointer status = new Memory(1);
    	this.readREG(AS7343.REG_CFG_0, status);
    	data = status.getByte(0);
    	data &= ~(1<<4);
    	this.writeREG(AS7343.REG_CFG_0, data);
    	
    	this.enableSpectralMeasure(false);
    	
    	data = 0x10;
    	this.writeREG(AS7343.REG_CFG_0, data);
    	
    	this.fd_confiig();
    	this.enableSMUX(true);
    	this.enableSpectralMeasure(true);
    	
    	this.enableFlickerDetection(true);
    	try {
			Thread.sleep(600*10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
    	status = new Memory(1);                    // 需不需要重新申请内存  这是一个问题
    	this.readREG(AS7343.REG_STATUS, status);
    	flicker = status.getByte(0);
    	this.enableFlickerDetection(false);
    	
    	switch (flicker) {
		case 44:
			flicker = 1;
			break;
		case 45:
			flicker = 50;
			break;
		case 46:
			flicker = 60;
			break;
		default:
			flicker = 0;
			break;
		}
    	
    	return flicker;
    }
    
    public byte getChannelData(int channel) {
    	Pointer data0 = new Memory(1);
    	Pointer data1 = new Memory(1);
    	byte data2b0   = 0;
    	byte data2b1   = 0;
		byte ch_data  = 0x0000;
		
		this.readREG((byte) (AS7343.REG_CH0_DATA_L + channel * 2), data0);
		this.readREG((byte) (AS7343.REG_CH0_DATA_L + channel * 2), data1);
		
		data2b0 =  data1.getByte(0);
		ch_data = data1.getByte(0);
		ch_data = (byte) ((ch_data << 8) | data2b0);
		
		// 确实很突兀 我也不知道为啥这时候要歇一会
		try {
			Thread.sleep(50 * 10);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ch_data;
	}
    
    public byte[] readSpectralDataOne() {
    	byte[] data = new byte[6];
    	data[0] = this.getChannelData(0);    // ADF1
    	data[1] = this.getChannelData(1);    // ADF2
    	data[2] = this.getChannelData(2);    // ADF3
    	data[3] = this.getChannelData(3);    // ADF4
    	data[4] = this.getChannelData(4);    // ADCLEAR
    	data[5] = this.getChannelData(5);    // ADNIR
    	return data;
    }
    
    public byte[] readSpectralDataTow() {
    	byte[] data = new byte[6];
    	data[0] = this.getChannelData(0);    // ADF5
    	data[1] = this.getChannelData(1);    // ADF6
    	data[2] = this.getChannelData(2);    // ADF7
    	data[3] = this.getChannelData(3);    // ADF8
    	data[4] = this.getChannelData(4);    // ADCLEAR
    	data[5] = this.getChannelData(5);    // ADNIR
    	return data;
    }
    
    public void setGPIOInterrupt() {
		// 该方法占时不实现
	}
    
    public void enableSysInt(boolean enable) {
    	byte data = (byte) 0x0;
    	
    	Pointer status = new Memory(1);
    	this.readREG(AS7343.REG_INTENAB, status);
    	data = status.getByte(0);
    	
    	if (enable) {
        	data |= (1 << 0);
		} else {
			data &= ~(1 << 0);
		}
    	
    	this.writeREG(AS7343.REG_INTENAB, data);
    }
    
    public void enableFIFOInit(boolean enable) {
    	byte data = (byte) 0x0;
    	
    	Pointer status = new Memory(1);
    	this.readREG(AS7343.REG_INTENAB, status);
    	data = status.getByte(0);
    	
    	if (enable) {
        	data |= (1 << 2);
		} else {
			data &= ~(1 << 2);
		}
    	
    	this.writeREG(AS7343.REG_INTENAB, data);
    }
    
    public void enableSpectralInt(boolean enable) {
    	byte data = (byte) 0x0;
    	
    	Pointer status = new Memory(1);
    	this.readREG(AS7343.REG_INTENAB, status);
    	data = status.getByte(0);
    	
    	if (enable) {
        	data |= (1 << 3);
		} else {
			data &= ~(1 << 3);
		}
    	
    	this.writeREG(AS7343.REG_INTENAB, data);
    }
    
    public void clearFIFO() {
    	byte data = (byte) 0x0;
    	
    	Pointer status = new Memory(1);
    	this.readREG(AS7343.REG_CONTROL, status);
    	data = status.getByte(0);
    	
        data |= (1 << 1);
        data &= ~(1 << 1);
		
    	this.writeREG(AS7343.REG_CONTROL, data);
    }
    
    public void autoZeroSpectral() {
    	byte data = (byte) 0x0;
    	
    	Pointer status = new Memory(1);
    	this.readREG(AS7343.REG_CONTROL, status);
    	data = status.getByte(0);
    	
        data |= (1 << 2);
		
    	this.writeREG(AS7343.REG_CONTROL, data);
    }
    
    public void enableFlickerInt(boolean enable) {
    	byte data = (byte) 0x0;
    	
    	Pointer status = new Memory(1);
    	this.readREG(AS7343.REG_INTENAB, status);
    	data = status.getByte(0);
    	
    	if (enable) {
        	data |= (1 << 2);
		} else {
			data &= ~(1 << 2);
		}
    	
    	this.writeREG(AS7343.REG_INTENAB, data);
    }
    
    public void setAtime(byte value) {
    	this.writeREG(AS7343.REG_ATIME, value);
    }
    
    public void setAgain(byte value) {
    	if (value > 10) {
			value = 10;
		}
    	this.writeREG(AS7343.REG_CFG_1, value);
    }
    
    public void setAstep(byte value) {
    	byte high_value, low_value;
    	low_value = (byte) (value & 0x00ff);
    	high_value = (byte) (value >> 8);
    	this.writeREG(AS7343.REG_ASTEP_L, low_value);
    	this.writeREG(AS7343.REG_ASTEP_H, high_value);
    }
        
    public float getIntegrationTime() {
    	byte data = (byte) 0x0;
    	byte[] astepData = {0, 0};
    	byte astep = (byte) 0x0;
    	
    	Pointer dataP = new Memory(1);
    	Pointer dataPa0 = new Memory(1);
    	Pointer dataPa1 = new Memory(1);
    	this.readREG(AS7343.REG_ATIME, dataP);
    	this.readREG(AS7343.REG_ASTEP_L, dataPa0); // ?
    	this.readREG(AS7343.REG_ASTEP_L, dataPa1); // ? 一个L 一个H吧？
    	astepData[0] = dataP.getByte(0);
    	astepData[1] = dataPa0.getByte(0);
    	
    	astep = astepData[1];
    	astep = (byte) ((astep << 8) | astepData[0]);
    	if (data == 0) {
			
		} else if (data > 0 && data < 255) {
			
		} else if (data == 255) {
			
		}
    	
    	return 0; // 明显未完工 以后再说
    }
    
    public void setWtime(byte value) {
    	this.writeREG(AS7343.REG_WTIME, value);
    }
    
    public float getWtime() {
    	float value = 0;
    	byte data = (byte) 0x0;
    	Pointer status = new Memory(1);
    	this.readREG(AS7343.REG_WTIME, status);
    	data = status.getByte(0);
    	if (data == 0) {
        	value = (float) 2.78;
		} else if (data == 1) {
			value = (float) 5.56;
		} else if (data > 1 && data < 255) {
			value = (float) (2.78 * (data + 1));
		} else if (data == 255) {
			value = 711; // you sure?
		}
    	
		return value;	
    }
    
    public void setThreshold(byte low_threshold, byte high_threshold) {
    	if (low_threshold >= high_threshold) {
			return;
		}
    	
    	this.writeREG(AS7343.REG_SP_TH_H_MSB, (byte) (high_threshold >> 8));
    	this.writeREG(AS7343.REG_SP_TH_H_LSB, high_threshold);
    	
    	this.writeREG(AS7343.REG_SP_TH_L_MSB, (byte) (low_threshold >> 8));
    	this.writeREG(AS7343.REG_SP_TH_L_LSB, low_threshold);
    	
    	try {
			Thread.sleep(20 * 10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
    }
    
    public byte getLowThreshold() {
    	byte thdata = (byte) 0x0000;
    	byte[] data = {0, 0};
    	
    	Pointer dataP0 = new Memory(1);
    	Pointer dataP1 = new Memory(1);
    	this.readREG(AS7343.REG_SP_TH_L_MSB, dataP0);
    	this.readREG(AS7343.REG_SP_TH_L_LSB, dataP1);
    	data[0] = dataP0.getByte(0);
    	data[1] = dataP1.getByte(0);
    	
    	thdata = data[1];
    	thdata = (byte) ((thdata << 8) | data[0]);
    	
    	return thdata;
    }
    
    public byte getHighThreshold() {
    	byte thdata = (byte) 0x0000;
    	byte[] data = {0, 0};
    	
    	Pointer dataP0 = new Memory(1);
    	Pointer dataP1 = new Memory(1);
    	this.readREG(AS7343.REG_SP_TH_H_MSB, dataP0); // ?
    	this.readREG(AS7343.REG_SP_TH_H_LSB, dataP1); // ? 一个L 一个H吧？
    	data[0] = dataP0.getByte(0);
    	data[1] = dataP1.getByte(0);
    	
    	thdata = data[1];
    	thdata = (byte) ((thdata << 8) | data[0]);
    	
    	return thdata;
    }
    
    public void cleanInterrupt() {
		this.writeREG(AS7343.REG_STATUS_1, (byte) 0xff);
	}
    
    public void setChannelInt(byte channel) {
    	byte data = 0;
    	
    	if (channel >= 5) {
			return;
		}
    	
    	Pointer status = new Memory(1);
    	this.readREG(AS7343.REG_CFG_12, status);
    	data = status.getByte(0);
    	
    	data &= ~(7);
    	data |= channel;
    	this.writeREG(AS7343.REG_CFG_12, data);
	}
    
    public void setApers(byte value) {
    	byte high_value, low_value;
    	low_value = (byte) (value & 0x00ff);
    	high_value = (byte) (value >> 8);
    	this.writeREG(AS7343.REG_ASTEP_L, low_value);
    	this.writeREG(AS7343.REG_ASTEP_H, high_value);
    }
    
    public byte getIntSource() {
    	Pointer id = new Memory(1);
    	if (this.readREG(AS7343.REG_STATUS_3, id)) {
			return id.getByte(0);
		}
    	return -1;
    }
    
    public boolean getInterrupt() {
    	byte data = (byte) 0x0;
    	Pointer status = new Memory(1);
    	if (this.readREG(AS7343.REG_STATUS_1, status)) {
        	data = status.getByte(0);
		}
    	if ((data & 0x80) == 1) {
			return true;
		} else {
			return false;
		}
    }
    
    public boolean checkWtime() {
    	byte value = (byte) 0x0;
    	Pointer status = new Memory(1);
    	if (this.readREG(AS7343.REG_STATUS_6, status)) {
        	value = status.getByte(0);
		}
    	value = (byte) (value & (1 << 2));
    	if (value > 0) {
			return false;
		} else {
			return true;
		}
    }
    
    public void init(int measure_mode) {

    	this.measure_mode = measure_mode;
    	this.enable(true);
    	
    	//System.out.println(readID());
    	
    }
    
    public void deinit() {
    	
    	// 寄存器状态
    	
    }
    
}
